//
//  HSTabBarItemView.swift
//  HSMainViewControllerHandler
//
//  Created by haisheng huang on 15/10/29.
//  Copyright © 2015年 huanghaisheng. All rights reserved.
//

import UIKit

/*样式*/
public enum HSTabItemStyle {
    case title
    case titleAndIcon
}

/*TabItemInfoKey swift3.0 方式*/
public enum TabItemInfoKey {
    case title
    case normalImage
    case highlightedImage
    case normalColor
    case highlightedColor
}

/*delegate*/
@objc public protocol HSTabBarItemViewDelegate: NSObjectProtocol {
    @objc optional func shouldClickedTabBarItemView(_ view: HSTabBarItemView) -> Bool
    @objc optional func didClickedTabBarItemView(_ view: HSTabBarItemView)
}

//oc转swift方式
//public let TabItemInfoKeyTitle: String = "Title"
//public let TabItemInfoKeyNormalImage: String = "NormalImage"
//public let TabItemInfoKeyHighlightedImage: String = "HighlightedImage"
//public let TabItemInfoKeyNormalColor: String = "NormalColor"
//public let TabItemInfoKeyHighlightedColor: String = "HighlightedColor"

open class HSTabBarItemView: UIView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    
    var iconView: UIImageView?
    var titleLabel: UILabel?
    var button: UIButton?
    var highlightedImage: UIImage?
    var normalImage: UIImage!
    var title: String?
    var normalColor: UIColor?
    var highlightedColor: UIColor?
    var itemStyle: HSTabItemStyle?
    var delegate: HSTabBarItemViewDelegate?
    
    var iconSizeLength: CGFloat? = 30.0 //default 30.0
    var titleHeight: CGFloat? = 16.0 //default 16.0
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        self.delegate = nil
    }
    
    convenience init(frame: CGRect, itemInfo: NSDictionary?, itemStyle: HSTabItemStyle) {
        
        self.init(frame: frame)
        
        
        self.itemStyle = itemStyle
        self.title = itemInfo![TabItemInfoKey.title] as? String
        self.normalImage = self.getIconImage(itemInfo![TabItemInfoKey.normalImage] as AnyObject)
        self.highlightedImage = self.getIconImage(itemInfo![TabItemInfoKey.highlightedImage] as AnyObject)
        self.normalColor = self.getItemTextColor(itemInfo![TabItemInfoKey.normalColor] as AnyObject)
        self.highlightedColor = self.getItemTextColor(itemInfo![TabItemInfoKey.highlightedColor] as AnyObject)
        
        if itemStyle == HSTabItemStyle.title {
            
            let button = UIButton(type: UIButtonType.custom)
            button.frame.size = frame.size
            button.addTarget(self, action: #selector(HSTabBarItemView.clickedTabBar(_:)), for: UIControlEvents.touchUpInside)
            self.addSubview(button)
            self.button = button
            
            let titleLabel = UILabel()
            titleLabel.frame.size = frame.size
            titleLabel.font = UIFont.systemFont(ofSize: 17.0)
            titleLabel.textAlignment = NSTextAlignment.center
            titleLabel.textColor = self.normalColor
            titleLabel.text = self.title
            self.addSubview(titleLabel)
            self.titleLabel = titleLabel
            
        } else if itemStyle == HSTabItemStyle.titleAndIcon {
            
            let button = UIButton(type: UIButtonType.custom)
            button.frame.size = frame.size
            button.addTarget(self, action: #selector(HSTabBarItemView.clickedTabBar(_:)), for: UIControlEvents.touchUpInside)
            self.addSubview(button)
            self.button = button
            
            let iconView = UIImageView()
            iconView.frame = CGRect(x: frame.width/2.0 - self.iconSizeLength!/2.0, y: 3.0, width: self.iconSizeLength!, height: self.iconSizeLength!)
            iconView.image = self.normalImage
            self.addSubview(iconView)
            self.iconView = iconView
            
            let titleLabel = UILabel()
            titleLabel.frame = CGRect(x: 0.0, y: frame.height - self.titleHeight!, width: frame.width, height: self.titleHeight!)
            titleLabel.font = UIFont.systemFont(ofSize: 10.0)
            titleLabel.textAlignment = NSTextAlignment.center
            titleLabel.textColor = self.normalColor
            titleLabel.text = self.title
            self.addSubview(titleLabel)
            self.titleLabel = titleLabel
            
        }
        
    }
    
    override open func layoutSubviews() {
        super.layoutSubviews()
        let frame = self.frame
        if self.itemStyle == HSTabItemStyle.titleAndIcon {
            self.button?.frame = CGRect(x: 0.0, y: 0.0, width: frame.width, height: frame.height)
            self.iconView?.frame = CGRect(x: frame.width/2.0 - self.iconSizeLength!/2.0, y: 3.0, width: self.iconSizeLength!, height: self.iconSizeLength!)
            self.titleLabel?.frame =  CGRect(x: 0.0, y: frame.height - self.titleHeight!, width: frame.width, height: self.titleHeight!)
        } else if self.itemStyle == HSTabItemStyle.title {
            self.button?.frame = CGRect(x: 0.0, y: 0.0, width: frame.width, height: frame.height)
            self.titleLabel?.frame = CGRect(x: 0.0, y: 0.0, width: frame.width, height: frame.height)
            
        }
    }
    
    /*设置item的正常状态*/
    func setNormalStatus() {
        self.titleLabel?.textColor = self.normalColor
        if self.itemStyle == HSTabItemStyle.titleAndIcon && self.normalImage != nil {
            self.iconView?.image = self.normalImage
        }
    }
    
    /*设置item的高亮状态*/
    func setHighlightedStatus() {
        
        self.titleLabel?.textColor = self.highlightedColor
        if self.itemStyle == HSTabItemStyle.titleAndIcon && self.highlightedImage != nil {
            self.iconView?.image = self.highlightedImage
        }
    }
    
    /*手动设置点击item*/
    func clickMe() {
        self.clickedTabBar(nil)
    }
    
    /*item button点击action*/
    func clickedTabBar(_ sender:UIButton?) {
        
        if ((self.delegate?.responds(to: #selector(HSTabBarItemViewDelegate.shouldClickedTabBarItemView(_:)))) != nil) {
            if ((self.delegate?.shouldClickedTabBarItemView!(self)) != false) {
                self.setHighlightedStatus()
                self.delegate?.didClickedTabBarItemView!(self)
            }
        } else {
            self.setHighlightedStatus()
            self.delegate?.didClickedTabBarItemView!(self)
        }
    }
    
    /*获取icon image*/
    fileprivate func getIconImage(_ image:AnyObject?) -> UIImage? {
        if image is UIImage {
            return image as? UIImage
        } else if image is String {
            return UIImage(named: image as! String)!
        } else {
            return nil
        }
    }
    
    /*获取颜色UIColor*/
    fileprivate func getItemTextColor(_ color:AnyObject?) -> UIColor? {
        if color is UIColor {
            return color as? UIColor
        } else if color is String {
            return self.convertToColor(with: color as! String)
        } else {
            return nil
        }
    }
   
    
    /*十六进制字符串转换UIColor*/
    func convertToColor(with hexString: String) -> UIColor {
        
        var cString: String = hexString.trimmingCharacters(in: CharacterSet.whitespaces).uppercased()
        
        if cString.characters.count < 6 {
            return UIColor.white
        }
        
        if cString.hasPrefix("0X") {
            cString = cString.substring(from: cString.index(cString.startIndex, offsetBy: 2))
        }
        
        if cString.hasPrefix("#") {
            cString = cString.substring(from: cString.index(cString.startIndex, offsetBy: 1))
        }
        
        if cString.characters.count != 6 {
            return UIColor.white
        }
        
        var tempRange: Range = cString.startIndex..<cString.index(cString.startIndex, offsetBy: 2)
        let rString: String = cString.substring(with: tempRange)
        tempRange = cString.index(cString.startIndex, offsetBy: 2)..<cString.index(cString.startIndex, offsetBy: 4)
        let gString: String = cString.substring(with: tempRange)
        tempRange = cString.index(cString.startIndex, offsetBy: 4)..<cString.index(cString.startIndex, offsetBy: 6)
        let bString: String = cString.substring(with: tempRange)
        
        var red: uint = 0
        var green: uint = 0
        var blue: uint = 0
        
        Scanner(string: rString).scanHexInt32(&red)
        Scanner(string: gString).scanHexInt32(&green)
        Scanner(string: bString).scanHexInt32(&blue)
        
        
        return UIColor.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0)
        
    }

    
}
